package com.ailanyin.soft.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 安装 Nginx
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-1-10 13:45:03
 */
public class NginxUtil {

    private static void createLatestNginxRepo() {
        System.out.println("========== 开始创建 Nginx repo ==========");
        List<String> list = new ArrayList<>();
        list.add("[nginx-stable]");
        list.add("name=nginx stable repo");
        list.add("baseurl=http://nginx.org/packages/centos/$releasever/$basearch/");
        list.add("gpgcheck=1");
        list.add("enabled=1");
        list.add("gpgkey=https://nginx.org/keys/nginx_signing.key");
        list.add("module_hotfixes=true");

        FileUtil.writeLines(list,"/etc/yum.repos.d/nginx.repo","UTF-8",false);
        System.out.println("========== 成功创建 Nginx repo ==========");
    }

    public static void installNginx() {
        createLatestNginxRepo();
        System.out.println("========== 开始安装 Nginx ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("yum install nginx -y"));
        System.out.println("========== 成功安装 Nginx ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("systemctl restart nginx"));
        System.out.println("========== Nginx 启动完成 ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("systemctl enable nginx"));
        System.out.println("========== 设置 Nginx 开机自启完成 ==========");
    }
}
